/**
* 我的比赛页
* path:'/game/mygames' ,query:{tab:'可选'}
* tab='play'，默认显示我参加的比赛tab；tab='build',默认显示我创建的比赛tab
*/
<template>
  <div class="game-my">
    <div id="list" class="gamemy-list">

      <header class="common-app-header">
        <div class="common-app-btn-back" @click="_back()"><i class="iconfont icon-fanhui"></i></div>
        <div class="common-app-title">
          <span class="title allgame" @click="_allGame">所有比赛</span><span class="title mygame active">我的比赛</span>
        </div>
        <div class="common-app-right-btns">
          <div class="common-app-btn-share" @click="_share()"><i class="iconfont icon-fenxiang"></i></div>
        </div>
      </header>

      <ul class="nav nav-tabs">
        <li :class="{'active':tab=='play'}" @click="_clickTab1"><a class="play" data-toggle="tab"><i
          class="iconfont icon-cansai"></i>我参加的比赛</a></li>
        <li :class="{'active':tab=='build'}" @click="_clickTab2"><a class="build" data-toggle="tab"><i
          class="iconfont icon-chuangsai"></i>我创建的比赛</a></li>
      </ul>

      <div class="tab-content">
        <game-play :tab="tab" :userCode="userCode" :isLogin="isLogin"></game-play>
        <game-build :tab="tab" :userCode="userCode" :isLogin="isLogin"></game-build>
      </div>

    </div>
  </div>
</template>

<script>
  import Constant from 'common/constant'
  import LocalDAO from 'common/localDAO'
  import RemoteDAO from 'common/remoteDAO'
  import GamePlay from 'components/game/my/GamePlay'
  import GameBuild from 'components/game/my/GameBuild'
  import $ from 'jquery'

  export default {
    name: 'MyGames',
    data () {
      return {
        tab: this.$route.query.tab || 'play',  //play显示我参加的比赛 build显示我创建的比赛
        isLogin: false,
        userCode: LocalDAO.getUserCode()
      }
    },
    created: function () {
      if (LocalDAO.getNickname()) {
        this.isLogin = true;
      }
    },
    components: {
      "game-play": GamePlay,
      "game-build": GameBuild
    },
    methods: {
      _clickTab1(){
        this.tab = 'play';
      },
      _clickTab2(){
        this.tab = 'build';
      },
      _back(){
        if (typeof (this.$route.query.cleanStorage) !== 'undefined') {
          window.location.href = "gtjanormal://goback";
        } else{
          this.$router.go(-1);
        }
      },
      _share(){
        var self = this;

        var kv = {"usercode": self.userCode};
        TDAPP.onEvent("1010", "", kv);
        TDAPP.send();

        var game_play_data = LocalDAO.getGamePlay();
        var game_build_data = LocalDAO.getGameBuild();
        var data = {
          "game_play_data": game_play_data,
          "game_build_data": game_build_data,
          "tab": self.tab
        };
        data = JSON.stringify(data);

        $.ajax({
          url: "https://dl.app.gtja.com/yyz/data/api/niurenshare/share",
          type: "POST",
          dataType: "json",
          contentType: 'application/json;charset=UTF-8',
          data: JSON.stringify({usercode: self.userCode, type: "mygame", content: data}),
          success: function (response) {
            console.log(JSON.stringify(response));
            // window.location.href = "share.gamemy.html?url="+response.url+"&userCode="+self.userCode; //直接跳转，测试用
            var title = "沉迷比赛无法自拔";
            var content = "来啊，互相伤害";
            var linkurl = Constant.url.CDN + "share.gamemy.html?url=" + response.url + "&userCode=" + self.userCode;
            var moments = "all";
            var icon = "https://dl.app.gtja.com/nrng/shareimages/bisai1.png";
            RemoteDAO.share(title, content, linkurl, moments, icon);
          },
          error: function (e) {
            alert("error");
          }
        });
      },
      _allGame(){
        this.$router.push({path: '/game'})
      }
    }
  }
</script>

<style scoped>

</style>
